import {
  request
} from "../../request/index.js";
var app = getApp();
var util = require("../../utils/util.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {
      openid: ""
    },
    user: {},
    nickName: "",
    avatarUrl: ""
  },
  login() {
    wx.navigateTo({
      url: '/pages/personalTwo/personalTwo?userId=' + this.data.user.userId,
    })
   
  },

  async getPhoneNumber(e) {
    try {
      const { code } = e.detail
      const res = await app.goLoginPub(code)
      if (res) {
        this.setData({
          user: wx.getStorageSync('user'),
        })
      }

    } catch (error) {
      console.log('error :>> ', error);
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("个人页面刷新完成")
    if (options.isshare == 1) {
      wx.navigateTo({
        url: '../guang/guang'
      })
    }
    var that = this
    var user = wx.getStorageSync('user')
    this.setData({
      user:user
    })
    var d=user.memberDueDate
    d=new Date(d.replace(/-/g,"/"))
    var dd=new Date();
    var day=parseInt((d.getTime()-dd.getTime())/(1000*60*60*24))
    if(day>=0&&day<3){
      console.log(day)
    }
    
    console.log(dd)
    if (!user) {
      wx.login({
        success: function (res) {
          request({
            url: '/UserInfo/getCode',
            data: {
              code: res.code
            }
          }).then(r => {
            var openid = "userInfo.openid"
            that.setData({
              [openid]: r.data
            })
          }).then(re => {
            request({
              url: "/UserInfo/getAllUserInfoByType",
              data: {
                openid: that.data.userInfo.openid
              }
            }).then(rs => {
              var u = wx.getStorageSync('u');
              if (!u || rs.data.list.length == 0) {

                wx.getUserProfile({
                  desc: "用于完善用户信息",
                  success: (r) => {
                    console.log(r)
                    wx.setStorageSync('u', r.userInfo)
                    that.setData({
                      nickName: r.userInfo.nickName,
                      avatarUrl: r.userInfo.avatarUrl
                    })
                    wx.uploadFile({
                      url: 'https://www.chengkoo.cn/images/metal/addPic',
                      filePath: that.avatarUrl,
                      name: "file",
                      success: function (res) {
                        request({
                          url: "/UserInfo/addUserInfo",
                          data: {
                            openid: that.data.userInfo.openid,
                            nickName: that.data.nickName,
                            userProfile:that.data.avatarUrl
                          }
                        }).then(a => {
                          request({
                            url: "/UserInfo/getAllUserInfoByType",
                            data: {
                              openid: that.data.userInfo.openid
                            }
                          }).then(ss => {
                            app.globalData.userId = ss.data.list[0].userId;
                            wx.setStorageSync('user', ss.data.list[0]);
                            that.setData({
                              user: ss.data.list[0]
                            })
                          })
                        })
                      }
                    })
                  }
                })
              } else {
                app.globalData.userId = rs.data.list[0].userId;
                wx.setStorageSync('user', rs.data.list[0]);
                that.setData({
                  user: rs.data.list[0]
                })
              }
            })
          })
        }
      })
    } else {
      app.globalData.userId = user.userId;
      request({
        url: "/UserInfo/getUserInfo",
        data: {
          userId: user.userId
        }
      }).then(res => {
        that.setData({
          user: res.data
        })
      })

    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
   
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      path: '/pages/my/my?isshare=1',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  }
})